/**
 * Created by Administrator on 2018/10/24 0024.
 */
//用户文章信息侧边栏
//用户个人信息
function info_member(user_id) {
    sign.params={
        id : user_id
    };
    var encode_data = sign.encode_post();
    $.post(url_list.user_info,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);


        var html ='';
        if (data.code == 200) {
            if (data.data.img!=null && data.data.img!=''&& data.data.img!=undefined){
                document.getElementById("right_user_img").src = '/'+data.data.img;
            }
            document.getElementById("right_username").innerHTML = data.data.username;
            document.getElementById("introduce").innerHTML = data.data.introduce;
            html +='<a onclick="like_add(2,'+data.data.id+')" href="#l">' +
                '<button class="btn"><i class="fa fa-facebook" aria-hidden="true"></i> Like</button>' +
                '</a>'+
                '<a onclick="admin_archive('+data.data.id+')" href="#">' +
                '<button class="btn"><i class="fa fa-twitter" aria-hidden="true"></i> follow</button></a>';
            $('#post_info_change').html(html);

        } else {
            layer.msg(data.msg, {icon: 2});
        }
    })
}
//POST CATEGORIES文章模块
var post_user_id=[];
function post_article(user_id,keyword) {

    post_user_id.push(user_id);
    sign.params={
        userid : user_id,
        keyword:keyword
    };
    var encode_data = sign.encode_post();
    $.post(url_list.type_info,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);

        var html ='';
        if (data.code == 200) {
            if (data['data']['total'] === undefined || data['data']['total'].length == 0) {
                html ='';
                //页面数据替换
                $('#tihuan_post_type').html(html);
            }
            else {
                list = data.data.total;
                for (var i in list) {
                    list[i].type_id = list[i].type_id ? list[i].type_id : '';
                    list[i].count = list[i].count ? list[i].count : '';
                    list[i].type_name = list[i].type_name ? list[i].type_name : '';

                    html +='<li>';
                    html +='<a onclick="admin_archive(post_user_id[0],'+list[i].type_id+',1)" href="#" >'+list[i].type_name+' <span>'+list[i].count+'</span></a>';
                    html +='</li>'
                }
                $('#tihuan_post_type').html(html);

            }
        } else {
            layer.msg(data.msg, {icon: 2});
        }
    })
}

//POST ARCHIVE模块
function post_time(user_id,keyword) {
    sign.params={
        userid : user_id,
        keyword:keyword
    };
    var encode_data = sign.encode_post();
    $.post(url_list.time_info,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);
        var html ='';
        if (data.code == 200) {
            if (data['data']['total'] === undefined || data['data']['total'].length == 0) {
                html ='';
                //页面数据替换
                $('#tihuan_post_time').html(html);
            }
            else {
                list = data.data.total;
                for (var i in list) {
                    list[i].time = list[i].time ? list[i].time : '';
                    list[i].count = list[i].count ? list[i].count : '';

                    var key = list[i].time;

                    key = (key.replace('年','').replace('月',''));
                    html +='<li>';
                    html +='<a onclick="admin_archive(post_user_id[0],'+key+',2)" href="#">'+list[i].time+' <span>'+list[i].count+'</span></a>';
                    html +='</li>'
                }
                $('#tihuan_post_time').html(html);
            }
        } else {
            layer.msg(data.msg, {icon: 2});
        }
    })
}

//文章模块 list
function get_post_article_list(url,user_id,keyword,curr) {
    var currs = curr;
    post_user_id.push(user_id);
    sign.params={
        userid : user_id,
        keyword:keyword,
        curr:currs
    };
    var encode_data = sign.encode_post();
    layui.use(['laypage','form', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer
            ,form = layui.form;
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});

        $.post(url,encode_data, function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }

            datass = sign.decode_post(data);
            //替换
            post_article_tihuan(datass);
            console.log(datass);
            form.render('checkbox');
            layer.close(index);
            if(datass.data.pages<=1){
                $('#tihuan_post_list_page').html('');
            }
            else {
                //显示分页
                laypage.render({
                    elem: 'tihuan_post_list_page', 			// 容器
                    count: datass.data.count,     	// 总页数(后台的)
                    curr: datass.data.curr, 		//当前页(后台获取到的)
                    limit: datass.data.limit, 		//当前页(后台获取到的)
                    prev: '&laquo;',
                    next: '&raquo;',
                    jump: function (obj, first) {
                        if (!first) {
                            get_post_article_list(url,user_id,keyword,obj.curr);
                        }
                    }
                });
            }
        });
    });
}

//文章模块 替换
function post_article_tihuan(data) {
    var html ='';
    if (data.code == 200) {
        if (data['data']['total'] === undefined || data['data']['total'].length == 0) {
            html ='暂无发表文章';
            //页面数据替换
            $('#tihuan_post_list').html(html);
        }
        else {
            list = data.data.list;
            for (var i in list) {
                list[i].id = list[i].id ? list[i].id : '';
                list[i].title = list[i].title ? list[i].title : '';
                list[i].content = list[i].content ? list[i].content : '';
                list[i].likes = list[i].likes ? list[i].likes : 0;
                list[i].comment = list[i].comment ? list[i].comment : 0;
                list[i].img = list[i].img ? '/'+list[i].img : '__static__/img/asset/l1.jpg';
                list[i].time.day = list[i].time.day ?list[i].time.day:'';
                list[i].time.moth = list[i].time.moth ?list[i].time.moth:'';

                html +='<div class="single-list flex-row d-flex">'+
                    '<div class="thumb">'+
                    '<div class="date">';
                html += '<span>'+list[i].time.day+'</span><br>'+list[i].time.moth;
                html += '</div>';
                html += '<img style="width: 300px;height: 200px" src="'+list[i].img+'" alt="">';
                html += '</div>'+
                    '<div class="detail">';
                html += '<a href="#" onclick="articleInfo('+list[i].id+')"><h4 class="pb-20">'+list[i].title+'</h4></a>';
                html +='<p>'+short_content(list[i].content)+ '</p>';
                html +='<p class="footer pt-20">'+
                    '<i class="fa fa-heart-o" aria-hidden="true"></i>';
                html += '<a href="#">'+list[i].likes+' Likes</a>     ' +
                    '<i class="ml-20 fa fa-comment-o" aria-hidden="true"></i> ' +
                    '<a href="#">'+list[i].comment+' Comments</a>';
                html +='</p> </div> </div>';

            }
            $('#tihuan_post_list').html(html);
        }
    } else {
        layer.msg(data.msg, {icon: 2});
    }
}

